package lanou.giftsay_01.fragment.body;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import lanou.giftsay_01.adapter.MyLvadapter;
import lanou.giftsay_01.bean.GuideBean;
import lanou.giftsay_01.main.hotmain.HotSecondActivity;
import lanou.giftsay_01.tools.BaseFragment;
import lanou.giftsay_01.tools.NetTool;
import lanou.giftsay_01.tools.URLValues;
import lanou.giftsay_01.R;

/**
 * Created by dllo on 16/8/15.
 * fragment的复用
 */
public class BoyfriendFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener{
    private ListView listView;
    private ArrayList<GuideBean> arrayList;
    private SwipeRefreshLayout swipeRefreshLayout;
    private Bundle bundle;
    private String url;
    private NetTool netTool;

    public static BoyfriendFragment getInstance(String url) {
        Bundle bundle = new Bundle();
        bundle.putString("key", url);
        BoyfriendFragment boyfriendFragment = new BoyfriendFragment();
        boyfriendFragment.setArguments(bundle);
        return boyfriendFragment;
    }

    @Override
    protected int setLayout() {
        return R.layout.select_fragment;
    }

    @Override
    protected void initView() {
        listView = (ListView)getView().findViewById(R.id.select_lv);
        /**
         * 下来刷新
         */
        swipeRefreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.swipe_container);
    }

    @Override
    protected void initData() {
        bundle = getArguments();
        url = bundle.getString("key");
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);   // 设置下拉圆圈上的颜色，蓝色、绿色、橙色、红色
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);// 设置圆圈的大小
        swipeRefreshLayout.setDistanceToTriggerSync(400);// 设置手指在屏幕下拉多少距离会触发下拉刷新
        swipeRefreshLayout.setOnRefreshListener(this);
        netTool = new NetTool();
        netTool.getNetData(url, GuideBean.class, new NetTool.NetListener<GuideBean>() {
            @Override
            public void onSuccess(GuideBean guideBean) {
                MyLvadapter myLvadapter = new MyLvadapter(mContext);
                arrayList = new ArrayList<GuideBean>();
                arrayList.add(guideBean);
                myLvadapter.setArrayList(arrayList);
                listView.setAdapter(myLvadapter);
                /**
                 * 设置监听,接口回调
                 */
                myLvadapter.setOnClickGuideItem(new OnClickGuideItem() {
                    @Override
                    public void onClick(int position, ArrayList<GuideBean> arrayList) {
                        Intent intent = new Intent(BoyfriendFragment.this.getContext() , HotSecondActivity.class);
                        intent.putExtra("key" , arrayList.get(0).getData().getItems().get(position).getUrl());
                        startActivity(intent);
                    }
                });
//
            }
            @Override
            public void onError(String errorMsg) {

            }
        });

    }

    /**
     * 设置监听
     */

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {


                netTool.getNetData(url, GuideBean.class, new NetTool.NetListener<GuideBean>() {
                    @Override
                    public void onSuccess(GuideBean guideBean) {
                        MyLvadapter myLvadapter = new MyLvadapter(mContext);
                        arrayList = new ArrayList<GuideBean>();
                        arrayList.add(guideBean);
                        myLvadapter.setArrayList(arrayList);
                        // 停止刷新
                        swipeRefreshLayout.setRefreshing(false);
                    }
                    @Override
                    public void onError(String errorMsg) {

                    }
                });

            }
        }, 5); // 5秒后发送消息，停止刷新

    }
}
